package com.ruoyi.loans.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.loans.feign.AgreementFeign;
import com.ruoyi.loans.feign.RepaymentFeign;
import com.ruoyi.loans.mapper.LoanCheckInfoMapper;
import com.ruoyi.loans.pojo.Agreement;
import com.ruoyi.loans.pojo.LoanCheckInfo;
import com.ruoyi.loans.pojo.Result;
import com.ruoyi.loans.pojo.SearchVo;
import com.ruoyi.loans.service.LoanCheckInfoService;
import com.ruoyi.loans.utils.IdWorker;
import com.ruoyi.repay.domain.RepaymentDetails;
import com.ruoyi.repay.domain.RepaymentSchedule;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoanCheckInfoServiceImpl extends ServiceImpl<LoanCheckInfoMapper, LoanCheckInfo> implements LoanCheckInfoService {
    @Autowired
    private LoanCheckInfoMapper loanCheckInfoMapper;

    @Autowired
    private RepaymentFeign repaymentFeign;
    @Autowired
    private AgreementFeign agreementFeign;

    @Override
    public void add(LoanCheckInfo loanCheckInfo) {
        loanCheckInfo.setCreateTime(LocalDateTime.now());
        loanCheckInfo.setUpdateTime(LocalDateTime.now());
        loanCheckInfoMapper.add(loanCheckInfo);
    }

    @Override
    public Result findLoanCheckList(SearchVo searchVo) {

        //分页
        Page<LoanCheckInfo> page = new Page<>(searchVo.getCurrent(), searchVo.getSize());
        Page<LoanCheckInfo> p = this.baseMapper.selectPage(page, null);
        //把数据给到records
        p.setRecords(p.getRecords());
        Map map = new HashMap<>();
        map.put("total", p.getTotal());
        map.put("data", p.getRecords());
        return new Result(200, "查询成功", map);
    }

    @Override
    public Result delById(Integer id) {
        LoanCheckInfo loanCheckInfo = this.baseMapper.selectById(id);
        if (loanCheckInfo != null && loanCheckInfo.getIsStatus() == 3) {
            return new Result(400, "该数据不能删除", null);
        } else if (loanCheckInfo != null) {
            this.baseMapper.delById(id);
            return new Result(200, "删除成功", null);
        } else {
            return new Result(400, "数据不存在", null);
        }
    }

    @Override
    @GlobalTransactional
    public Result updateIsStatus(LoanCheckInfo loanCheckInfo) {

//        //随机生成code
//        IdWorker idWorker = new IdWorker();
//        String code = Long.toString(idWorker.nextId());
//        loanCheckInfo.setCode(code);
        //修改为最新的时间
        loanCheckInfo.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(loanCheckInfo);
        LoanCheckInfo checkInfo = this.baseMapper.selectById(loanCheckInfo.getId());
        Agreement agreementCode = agreementFeign.getAgreementCode(loanCheckInfo.getUserCode());
        int i = agreementCode.getNum().compareTo(loanCheckInfo.getDownMoney());
        if (i>0 || i==0){
            if(checkInfo.getIsStatus() == 1){
                RepaymentSchedule repaymentSchedule = new RepaymentSchedule();
                BeanUtils.copyProperties(checkInfo, repaymentSchedule);
                repaymentSchedule.setCheckCode(checkInfo.getApplyForLoanCode());
                repaymentSchedule.setUserInfoCode(checkInfo.getUserCode());
                repaymentSchedule.setPrice(checkInfo.getRepaymentPrice());
                repaymentFeign.addrepaymentschedule(repaymentSchedule);
                RepaymentDetails repaymentDetails = new RepaymentDetails();
                BeanUtils.copyProperties(checkInfo, repaymentDetails);
                repaymentDetails.setCheckCode(checkInfo.getApplyForLoanCode());
                repaymentDetails.setPrice(checkInfo.getRepaymentPrice());
                repaymentDetails.setTime(checkInfo.getUpdateTime());
                repaymentFeign.addrepaymentDetails(repaymentDetails);
                int a=1/0;
                Agreement agreement = new Agreement();
                agreement.setUserInformationInfoCode(loanCheckInfo.getUserCode());
                agreement.setDownMoney(loanCheckInfo.getDownMoney());
                agreementFeign.upAgreement(agreement);
                return new Result(200,"放款成功",null);
            }

        }else {
            return new Result(400,"放款失败,额度不足",null);
        }

        return new Result(200, "修改成功", null);
    }
}
